package com.shenchuangit.rydemo.web.client.base.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.shenchuangit.rydemo.web.client.base.domain.Client;
import com.shenchuangit.rydemo.web.client.base.mapper.ClientMapper;
import com.shenchuangit.rydemo.web.client.base.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户基础信息Service业务层处理
 *
 * @author Houxuzhong
 * @date 2021-10-09
 */
@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 查询客户基础信息
     *
     * @param id 客户基础信息ID
     * @return 客户基础信息
     */
    @Override
    public Client selectClientById(Long id) {
        return clientMapper.selectClientById(id);
    }

    /**
     * 查询客户基础信息列表
     *
     * @param client 客户基础信息
     * @return 客户基础信息
     */
    @Override
    public List<Client> selectClientList(Client client) {
        return clientMapper.selectClientList(client);
    }

    /**
     * 新增客户基础信息
     *
     * @param client 客户基础信息
     * @return 结果
     */
    @Override
    public Client insertClient(Client client) {
        client.setCreateTime(DateUtils.getNowDate());

        //权限相关字段赋值
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        client.setCreateBy(loginUser.getUserName());
        client.setAclCreateById(loginUser.getUserId());
        client.setAclDeptId(loginUser.getDeptId());
        //查询是否存在重复的客户名或证件号码
        selectDistinct(client);

        clientMapper.insertClient(client);
        return client;
    }

    /**
     * 查询是否存在重复的客户名或证件号码
     *
     */
    public Client selectDistinct(Client client){
        Client client1 = new Client();
        client1.setName(client.getName());
        Client client2 = new Client();
        client2.setCreditCode(client.getCreditCode());
        List<Client> clients1 = clientMapper.selectClientList(client1);
        List<Client> clients2 = clientMapper.selectClientList(client2);
        if(clients1 != null && clients1.size() > 0){
          throw new CustomException("有重复的客户名!");
        }else if(clients2 != null && clients2.size() > 0){
            throw new CustomException("有重复的证件号码!");
        }else{
            return client;
        }

    }

    /**
     * 修改客户基础信息
     *
     * @param client 客户基础信息
     * @return 结果
     */
    @Override
    public int updateClient(Client client) {
        client.setUpdateTime(DateUtils.getNowDate());

        //权限相关字段赋值
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        client.setUpdateBy(loginUser.getUserName());

        return clientMapper.updateClient(client);
    }

    /**
     * 批量删除客户基础信息
     *
     * @param ids 需要删除的客户基础信息ID
     * @return 结果
     */
    @Override
    public int deleteClientByIds(Long[] ids) {
        return clientMapper.deleteClientByIds(ids);
    }

    /**
     * 删除客户基础信息信息
     *
     * @param id 客户基础信息ID
     * @return 结果
     */
    @Override
    public int deleteClientById(Long id) {
        return clientMapper.deleteClientById(id);
    }


}
